Popular Searches
Popular Course Categories
Popular Courses

Strings, numbers, Boolean values, lists, and other data types

Strings, numbers, Boolean values, lists, and other data types

Introduction to Dart

Strings, Numbers, Boolean Values, Lists, and Other Data Types in Dart

Data types are one of the most important foundations of Dart programming. They define what kind of value a variable can store and help developers write structured, readable, and maintainable code. JustAcademy's Flutter curriculum includes Dart programming fundamentals such as variables, data types, operators, control statements, functions, and collections including List, Set, and Map. :contentReference[oaicite:0]{index=0}

Dart is the programming language used with Flutter to create applications for multiple platforms. Understanding strings, numbers, Boolean values, lists, sets, maps, nullable values, and other Dart types is essential before working with Flutter widgets, APIs, Firebase, databases, and application state.

Course: JustAcademy Flutter Training

Course Demo: Register for Flutter Course Demo


1. What Are Data Types in Dart?

A data type describes the kind of value that a variable contains. For example, a person's name is text, age is generally a whole number, price may contain a decimal value, and login status can be represented using a Boolean value.

String name = "Rahul";
int age = 25;
double price = 499.99;
bool isLoggedIn = true;

Here:

  • String stores text.
  • int stores whole numbers.
  • double stores decimal numbers.
  • bool stores true or false.

2. Main Data Types in Dart

Data Type Used For Example
String Text and characters "Hello"
int Whole numbers 25
double Decimal numbers 25.50
num Integer or decimal numbers 25.5
bool True/false values true
List Ordered collection [1, 2, 3]
Set Unique collection {1, 2, 3}
Map Key-value collection {"name": "Rahul"}
Object General Dart object type Object value = "Hello";
dynamic Flexible runtime type dynamic value = 10;
Null Represents absence of a value null

3. Strings in Dart

A String represents a sequence of characters and is used to store textual information.

Creating Strings

String name = "Rahul";
String city = 'Mumbai';
String course = "Flutter Training";

Dart supports both single and double quotation marks for strings.

String With Spaces

String message = "Welcome to Dart Programming";

4. String Concatenation

Concatenation means joining two or more strings together.

String firstName = "Rahul";
String lastName = "Sharma";

String fullName = firstName + " " + lastName;

print(fullName);

Output:

Rahul Sharma

5. String Interpolation

Dart provides string interpolation for inserting variable values directly into strings.

String name = "Amit";
int age = 25;

print("My name is $name");
print("I am $age years old");

For expressions, use ${}:

int price = 500;
int quantity = 3;

print("Total: ${price * quantity}");

Output:

Total: 1500

6. Multiline Strings

Triple quotation marks can be used when a string needs to contain multiple lines.

String message = '''
Welcome to Dart.
Learn Flutter.
Build mobile applications.
''';

print(message);

7. Useful String Properties

length

String name = "Flutter";

print(name.length);

Output:

7

isEmpty

String text = "";

print(text.isEmpty);

isNotEmpty

String text = "Hello";

print(text.isNotEmpty);

8. Common String Methods

String name = "Flutter";

print(name.toUpperCase());
print(name.toLowerCase());
print(name.contains("utt"));
print(name.startsWith("Flu"));
print(name.endsWith("ter"));

trim()

Removes unnecessary whitespace from the beginning and end of a string.

String name = "  Rahul  ";

print(name.trim());

replaceAll()

String message = "Hello World";

String result = message.replaceAll("World", "Dart");

print(result);

split()

String names = "Amit,Rahul,Priya";

List result = names.split(",");

print(result);

9. Numbers in Dart

Dart provides int, double, and num for working with numerical values.

10. int Data Type

The int type represents whole numbers.

int age = 25;
int marks = 90;
int quantity = 10;

Arithmetic With int

int a = 20;
int b = 10;

print(a + b);
print(a - b);
print(a * b);
print(a ~/ b);

Output:

30
10
200
2

11. double Data Type

The double type is used for numbers that can contain fractional or decimal values.

double price = 499.99;
double height = 5.8;
double percentage = 87.5;

Example

double price = 999.50;
double discount = 100.25;

double finalPrice = price - discount;

print(finalPrice);

12. num Data Type

The num type can represent both integer and decimal numeric values.

num value = 100;

value = 100.50;

print(value);

Use int when the value is specifically a whole number, double when a decimal value is appropriate, and num when either numeric form may be used.

13. Mathematical Operators

int a = 20;
int b = 6;

print(a + b);   // Addition
print(a - b);   // Subtraction
print(a * b);   // Multiplication
print(a / b);   // Division
print(a ~/ b);  // Integer division
print(a % b);   // Remainder

14. Boolean Values in Dart

The bool data type represents a Boolean value. A Boolean can be either true or false.

bool isLoggedIn = true;
bool isAdmin = false;
bool paymentCompleted = true;

15. Boolean Values With Conditions

bool isLoggedIn = true;

if (isLoggedIn) {
  print("Welcome!");
} else {
  print("Please login.");
}

16. Comparison Operators With Boolean Results

int age = 25;

print(age > 18);
print(age == 25);
print(age != 30);
print(age < 50);

Comparison expressions produce Boolean values.

17. Logical Operators

Dart provides logical operators such as:

  • && — logical AND
  • || — logical OR
  • ! — logical NOT

AND Operator

int age = 25;
bool hasId = true;

if (age >= 18 && hasId) {
  print("Access allowed");
}

OR Operator

bool isAdmin = false;
bool isManager = true;

if (isAdmin || isManager) {
  print("Access granted");
}

NOT Operator

bool isLoggedIn = false;

print(!isLoggedIn);

18. Lists in Dart

A List is an ordered collection of values. Lists are extremely useful in Flutter applications for displaying collections of data such as products, users, messages, categories, and notifications.

List fruits = [
  "Apple",
  "Banana",
  "Mango"
];

JustAcademy's Dart curriculum specifically includes collections such as List, Set, and Map. :contentReference[oaicite:1]{index=1}

19. Accessing List Items

Dart lists use zero-based indexing. The first item has index 0.

List fruits = [
  "Apple",
  "Banana",
  "Mango"
];

print(fruits[0]);
print(fruits[1]);
print(fruits[2]);

Output:

Apple
Banana
Mango

20. Adding Items to a List

List fruits = [
  "Apple",
  "Banana"
];

fruits.add("Mango");

print(fruits);

21. Adding Multiple Items

List fruits = [
  "Apple"
];

fruits.addAll([
  "Banana",
  "Mango",
  "Orange"
]);

print(fruits);

22. Removing Items From a List

List fruits = [
  "Apple",
  "Banana",
  "Mango"
];

fruits.remove("Banana");

print(fruits);

23. List Length

List fruits = [
  "Apple",
  "Banana",
  "Mango"
];

print(fruits.length);

Output:

3

24. Looping Through a List

List fruits = [
  "Apple",
  "Banana",
  "Mango"
];

for (String fruit in fruits) {
  print(fruit);
}

25. List of Numbers

List numbers = [
  10,
  20,
  30,
  40,
  50
];

print(numbers);

26. List of Objects

Lists can also contain objects created from classes.

class Student {
  String name;
  int age;

  Student(this.name, this.age);
}

void main() {
  List students = [
    Student("Amit", 20),
    Student("Priya", 21)
  ];

  print(students[0].name);
}

27. Set Data Type

A Set is a collection designed to contain unique values.

Set cities = {
  "Mumbai",
  "Delhi",
  "Pune"
};

Duplicate Values

Set numbers = {
  1,
  2,
  3,
  2,
  1
};

print(numbers);

Duplicate values are not retained as separate entries in a Set.

Adding to a Set

Set languages = {
  "Dart",
  "Java"
};

languages.add("Python");

print(languages);

28. Map Data Type

A Map stores values using key-value pairs.

Map user = {
  "name": "Rahul",
  "city": "Mumbai",
  "course": "Flutter"
};

29. Accessing Map Values

Map user = {
  "name": "Rahul",
  "city": "Mumbai"
};

print(user["name"]);
print(user["city"]);

Output:

Rahul
Mumbai

30. Adding Data to a Map

Map marks = {
  "Math": 90,
  "Science": 85
};

marks["English"] = 88;

print(marks);

31. Removing Data From a Map

Map marks = {
  "Math": 90,
  "Science": 85
};

marks.remove("Science");

print(marks);

32. Object Type

Object is a general Dart type that can refer to instances of different types.

Object value = "Hello";

print(value);

value = 100;

print(value);

Although Object can represent many values, operations that depend on a more specific type may require type checking or casting.

33. dynamic Type

The dynamic type provides greater runtime flexibility.

dynamic value = 10;

print(value);

value = "Hello";

print(value);

value = true;

print(value);

dynamic should be used carefully because excessive use can make code harder to analyze and can move type errors to runtime.

34. Null Values

null represents the absence of a value. Dart's null-safety system distinguishes nullable types from non-nullable types.

String? name;

name = null;

print(name);

The ? means that the variable is allowed to contain either a String or null.

35. Nullable Numbers

int? age;

age = null;

age = 25;

print(age);

36. final Variables

A final variable can be assigned once.

final String name = "Rahul";
final int age = 25;

After initialization, the variable cannot be assigned another value.

37. const Variables

const is used for compile-time constants.

const double pi = 3.14159;
const String appName = "My App";

38. final vs const

Feature final const
Can be assigned once Yes Yes
Compile-time constant Not necessarily Yes
Can be reassigned No No
Common use Runtime values that should not change Values known at compile time

39. Type Inference With var

Dart can automatically infer a variable's type from its initial value.

var name = "Rahul";
var age = 25;
var price = 499.99;
var active = true;

These values are inferred as String, int, double, and bool.

40. Generic Collections

Generics allow you to specify what type of data a collection should contain.

String List

List names = [
  "Amit",
  "Rahul",
  "Priya"
];

Integer List

List numbers = [
  10,
  20,
  30
];

String-to-Integer Map

Map marks = {
  "Math": 90,
  "Science": 85
};

41. Practical Example: Student Information

void main() {
  String name = "Amit";
  int age = 21;
  double percentage = 87.5;
  bool passed = true;

  List subjects = [
    "Math",
    "Science",
    "English"
  ];

  print("Name: $name");
  print("Age: $age");
  print("Percentage: $percentage");
  print("Passed: $passed");
  print("Subjects: $subjects");
}

42. Practical Example: Product Information

void main() {
  String productName = "Laptop";
  double price = 55000.00;
  int quantity = 2;
  bool available = true;

  double totalPrice = price * quantity;

  print("Product: $productName");
  print("Price: $price");
  print("Quantity: $quantity");
  print("Available: $available");
  print("Total Price: $totalPrice");
}

43. Practical Example: E-Commerce Data

void main() {
  Map product = {
    "name": "Smartphone",
    "price": 24999.99,
    "quantity": 3,
    "available": true,
    "categories": [
      "Electronics",
      "Mobile"
    ]
  };

  print(product["name"]);
  print(product["price"]);
  print(product["quantity"]);
  print(product["available"]);
  print(product["categories"]);
}

For larger applications, dedicated model classes and more specific types are generally preferable to using dynamic for everything.

44. Data Types in Flutter Applications

These Dart data types are used throughout Flutter development. JustAcademy's curriculum progresses from Dart fundamentals into widgets and UI, navigation, state management, REST APIs, Firebase, local storage, testing, and deployment. :contentReference[oaicite:2]{index=2}

For example, an application might use:

String userName = "Rahul";
int notificationCount = 5;
double walletBalance = 2500.50;
bool isLoggedIn = true;

List categories = [
  "Electronics",
  "Books",
  "Clothing"
];

Flutter widgets can then use these values to display information or control application behavior.

45. Choosing the Correct Data Type

Requirement Recommended Type Example
Name or text String String name = "Amit";
Age or count int int age = 25;
Price or percentage double double price = 99.99;
True/false condition bool bool active = true;
Ordered collection List List names = [];
Unique collection Set Set ids = {};
Key-value information Map Map marks = {};
Optional value Type? String? email;

46. Common Mistakes

Mistake 1: Assigning a String to an int

int age = 25;

// Incorrect:
// age = "Twenty Five";

Mistake 2: Accessing an Invalid List Index

List names = [
  "Amit",
  "Rahul"
];

// Valid indexes are 0 and 1
print(names[0]);
print(names[1]);

Mistake 3: Using dynamic Everywhere

// Avoid unnecessary dynamic usage
dynamic name = "Rahul";

Prefer a specific type when the type is known:

String name = "Rahul";

Mistake 4: Ignoring Nullability

String? username = null;

Code that handles nullable values should account for the possibility that the value is absent.

47. Best Practices

  • Use descriptive variable names.
  • Choose the most appropriate data type for each value.
  • Prefer specific types over unnecessary use of dynamic.
  • Use generic collections such as List.
  • Use final when a variable should not be reassigned.
  • Use const for compile-time constants.
  • Use nullable types only when null is a valid state.
  • Keep collection types consistent and predictable.
  • Use Boolean values for conditions rather than text such as "yes" and "no".

48. Quick Revision

  • String is used for text.
  • int is used for whole numbers.
  • double is used for decimal numbers.
  • num can represent integer or decimal numbers.
  • bool stores true or false.
  • List stores ordered collections.
  • Set stores unique values.
  • Map stores key-value pairs.
  • Object can refer to different Dart objects.
  • dynamic provides flexible runtime typing.
  • null represents the absence of a value.
  • Type? represents a nullable type.
  • final allows a variable to be assigned once.
  • const is used for compile-time constants.
  • var enables type inference.

49. Practice Exercises

  1. Create a String variable containing your full name.
  2. Create an int variable containing your age.
  3. Create a double variable containing the price of a product.
  4. Create a Boolean variable indicating whether a user is logged in.
  5. Create a List containing five programming languages.
  6. Create a Set containing five unique numbers.
  7. Create a Map containing student names and marks.
  8. Write a program that calculates the total price of three products.
  9. Create a nullable String variable and assign null to it.
  10. Create a small Dart program that combines String, int, double, bool, List, Set, and Map.

50. Complete Example

void main() {
  // String
  String studentName = "Rahul";

  // Integer
  int age = 22;

  // Decimal number
  double percentage = 88.5;

  // Boolean
  bool passed = true;

  // List
  List subjects = [
    "Dart",
    "Flutter",
    "Firebase"
  ];

  // Set
  Set skills = {
    "Programming",
    "UI Design",
    "API Integration"
  };

  // Map
  Map marks = {
    "Dart": 90,
    "Flutter": 85,
    "Firebase": 80
  };

  print("Name: $studentName");
  print("Age: $age");
  print("Percentage: $percentage");
  print("Passed: $passed");
  print("Subjects: $subjects");
  print("Skills: $skills");
  print("Marks: $marks");
}

51. Key Takeaways

Strings, numbers, Boolean values, lists, sets, maps, and other data types are essential building blocks of Dart programming. These concepts allow developers to represent real-world information in a structured way.

In Flutter development, these types are used constantly for user information, UI content, API responses, product data, application settings, form values, collections, and application state. JustAcademy's Flutter curriculum places Dart variables, data types, operators, and collections among its core programming fundamentals before moving into broader Flutter development topics. :contentReference[oaicite:3]{index=3}

52. Learn Flutter with JustAcademy

Explore the complete Flutter training program:

JustAcademy Flutter Training

To register for a Flutter course demo:

Register for Flutter Course Demo

whatsapp